// 本模块为首页视图相关逻辑及状态,主要用来做页面传参
import user from '@/api/user.js'
import {
  Message
} from 'element-ui'
const state = {
  userInfo: {},
  userCompanyInfo: {},
  userTeamInfo: {},
  userProjectInfo: {},
  companyNature: [],
  direction: [],
}

const getters = {
  userInfo(state) {
    return state.userInfo
  },
  userCompanyInfo(state) {
    return state.userCompanyInfo
  },
  userTeamInfo(state) {
    return state.userTeamInfo
  },
  userProjectInfo(state) {
    return state.userProjectInfo
  },
  companyNature(state) {
    return state.companyNature
  },
  direction(state) {
    return state.direction
  },
}

const mutations = {
  setUserState(state, {
    key,
    value
  }) {
    state[key] = value
  },
}
const actions = {
  // 个人信息
  getUserInfo({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.userCenterInfo().then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setUserState', {
              key: 'userInfo',
              value: res.data.data.user_info
            }),
            commit('setUserState', {
              key: 'userCompanyInfo',
              value: res.data.data.company_info
            }),
            commit('setUserState', {
              key: 'userTeamInfo',
              value: res.data.data.team_info
            }),
            commit('setUserState', {
              key: 'userProjectInfo',
              value: res.data.data.project_info
            }),
            resolve(res)
        }
      }).catch((error) => {
        localStorage.removeItem('token');
        localStorage.removeItem('password_status');
        localStorage.removeItem('password_status_message');
        reject(error)
      });
    })
  },
  // 修改个人信息
  updateUserInfo({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.userInfoUpdate({
        name: data.name,
        phone: data.phone,
        unit_position: data.unit_position,
        email: data.email,
        face_to_face: data.face_to_face,
        detailed_address: data.detailed_address,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setUserState', {
              key: 'userInfo',
              value: res.data.data.data
            }),
            resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 修改密码
  changePassword({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.changePassword({
        old_password: data.old_password,
        new_password: data.new_password,
        new_passwords: data.new_passwords,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 修改手机号
  changePhone({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.changePhone({
        password: data.password,
        new_phone: data.new_phone,
        phone_code: data.phone_code,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 上传文件
  uploadFile({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.uploadFile({
        file: data.file,
        type: data.type //1:企业logo; 2:报名表; 3:项目介绍; 4:项目图片; 5:展示视频; 6:其他
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  //  公司性质
  getCompanyNature({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.companyNature().then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setUserState', {
              key: 'companyNature',
              value: res.data.data
            }),
            resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 赛题方向
  getDirection({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.direction().then((res) => {
        let code = res.data.code;
        if (code == 200) {
          commit('setUserState', {
              key: 'direction',
              value: res.data.data
            }),
            resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 公司信息保存
  companySave({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.companySave({
        nature_id: data.nature_id,
        title: data.title,
        sub_title: data.sub_title,
        is_listed: data.is_listed,
        is_finance: data.is_finance,
        patent_count: data.patent_count,
        work_address: data.work_address,
        province: data.province,
        city: data.city,
        area: data.area,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 团队信息保存
  teamSave({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.teamSave({
        title: data.title,
        data: data.personnelList,
        delete_ids: data.delete_ids,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
  // 项目信息保存
  projectSave({
    state,
    dispatch,
    commit
  }, data) {
    return new Promise((resolve, reject) => {
      user.projectSave({
        direction_id: data.direction_id,
        app_count: data.app_count,
        title: data.title,
        is_patent: data.is_patent,
        is_online_events: data.is_online_events,
        content: data.content,
        logo_url: data.logo_url,
        enroll_form_url: data.enroll_form_url,
        pic_url: data.pic_url,
        ppt_url: data.ppt_url,
        video_url: data.video_url,
      }).then((res) => {
        let code = res.data.code;
        if (code == 200) {
          resolve(res)
        } else {
          Message.error(res.data.message);
        }
      }).catch((error) => {
        reject(error)
      });
    })
  },
}
export default {
  state,
  getters,
  actions,
  mutations
}
